//every table has a servers
//每一个表对应一个服务
//this is the sliderService for slider model
//第一个是轮播图的服务 Slider.js
const SliderModel = require('../db/models/slider');
//import model

class SliderService {
  async addSliderData (data) {
    const cid = data.cid;
    const result = await SliderModel.findOne({
      where: { cid }
    })

    if(result){
      return await SliderModel.update(data, {
        where: { cid }
      }); 
    }else {
      return await SliderModel.create(data);
    }
   
    //put the crawl data in the model and create
    //把数据作为参数创建模型
  }

  async getSliderData () {
    return await  SliderModel.findAll({
      attributes: {
        exclude: ['imgUrl', 'createdAt', 'updatedAt']
      }
    })
  }

  async changeSliderStatus (id, status) {
     //公用中间件控制器
     const result =  await SliderModel.update({ status }, {
      where: { id }
    })
    return result[0];
  }
}

module.exports = new SliderService();